package com.qsd.doc4jsimpple.item;

import cn.hutool.core.collection.CollectionUtil;
import com.qsd.doc4jsimpple.util.RichTextHelper;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.qsd.doc4jsimpple.util.RichTextHelper.replaceHttps2Http;

/**
 * * @Author xujunxia
 * * @Date 2024-11-04 19:45
 * * @Description TODO
 * * wb-0455@dtdream.com
 **/
public class DocxComponent {

    private String type;

    private Map<String,Object> attrs;

    private List<MarkStyle> marks;

    private List<DocxComponent> content;

    private Map<String,Object> value;

    private String text;

    private byte[] bytes;


    public Map<String, Object> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }

    public List<MarkStyle> getMarks() {
        return marks;
    }

    public void setMarks(List<MarkStyle> marks) {
        this.marks = marks;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<DocxComponent> getContent() {
        return content;
    }

    public void setContent(List<DocxComponent> content) {
        this.content = content;
    }

    public Map<String, Object> getValue() {
        return value;
    }

    public void setValue(Map<String, Object> value) {
        this.value = value;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


    public byte[] getBytes() {

        if(Objects.nonNull(getAttrs()) && Objects.nonNull(getAttrs().get("src"))){
            String url = getAttrs().get("src").toString();
            if(url.contains("https")){
                url = replaceHttps2Http(url);
            }
            byte[] bytes =  RichTextHelper.fetchFileFromServer(url);
            setBytes(bytes);
        }

        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public Object getAttr(String attrName) {
        if(CollectionUtil.isEmpty(attrs)){
            return null;
        }
        for(Map.Entry<String,Object> entry:attrs.entrySet()){
            if(entry.getKey().equals(attrName)){
                return entry.getValue();
            }
        }
        return null;
    }




}
